package com.gientech.Controller;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/rocketmq/provider")
public class ProviderController {

    @GetMapping("/produce")
    public String produce() throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer provider = new DefaultMQProducer("provider");
        provider.setNamesrvAddr("127.0.0.1:9876");
        provider.start();
        Message message = new Message("TopicTest","TagA",("hello rocketMq").getBytes(StandardCharsets.UTF_8));
        SendResult sendResult = provider.send(message);
        provider.shutdown();
        return sendResult.getSendStatus().toString();
    }
}
